<script setup>
	import tblCategoryApi from '@/api/biz/tblCategoryApi'
	import { TreeSelect } from 'ant-design-vue'
	const treeData = ref([])

	const emit = defineEmits(['updateSelectionConfig', 'updateQuestionCount', 'updateTotalScore'])

	const props = defineProps({
		selectionConfig: {
			type: String,
			required: true
		}
	})

	// 模拟 created 生命周期
	onBeforeMount(() => {
		tblCategoryApi.categoryTreeSelector({ type: 'library' }).then((res) => {
			treeData.value = res
		})
		try {
			const data = JSON.parse(props.selectionConfig)
			if (typeof data === 'object' && data !== null && !Array.isArray(data)) {
				selectionConfigLocal.value = data
			}
		} catch (e) {
			/* empty */
		}
	})
	const SHOW_PARENT = TreeSelect.SHOW_PARENT
	const selectionConfigLocal = ref({
		categoryIds: [],
		config: [
			{
				type: 'judge',
				name: '判断题',
				number: 0,
				score: 0,
				difficulty: false,
				difficulty_simple_number: 0,
				difficulty_simple_score: 0,
				difficulty_general_number: 0,
				difficulty_general_score: 0,
				difficulty_hard_number: 0,
				difficulty_hard_score: 0
			},
			{
				type: 'one',
				name: '单选题',
				number: 0,
				score: 0,
				difficulty: false,
				difficulty_simple_number: 0,
				difficulty_simple_score: 0,
				difficulty_general_number: 0,
				difficulty_general_score: 0,
				difficulty_hard_number: 0,
				difficulty_hard_score: 0
			},
			{
				type: 'multi',
				name: '多选题',
				number: 0,
				score: 0,
				difficulty: false,
				difficulty_simple_number: 0,
				difficulty_simple_score: 0,
				difficulty_general_number: 0,
				difficulty_general_score: 0,
				difficulty_hard_number: 0,
				difficulty_hard_score: 0
			},
			{
				type: 'fill',
				name: '填空题',
				number: 0,
				score: 0,
				difficulty: false,
				difficulty_simple_number: 0,
				difficulty_simple_score: 0,
				difficulty_general_number: 0,
				difficulty_general_score: 0,
				difficulty_hard_number: 0,
				difficulty_hard_score: 0
			},
			{
				type: 'simple',
				name: '简答题',
				number: 0,
				score: 0,
				difficulty: false,
				difficulty_simple_number: 0,
				difficulty_simple_score: 0,
				difficulty_general_number: 0,
				difficulty_general_score: 0,
				difficulty_hard_number: 0,
				difficulty_hard_score: 0
			}
		]
	})
	watch(
		selectionConfigLocal,
		(newValue) => {
			let score = 0
			let count = 0
			for (let i = 0; i < newValue.config.length; i++) {
				if (newValue.config[i].difficulty) {
					newValue.config[i].number =
						newValue.config[i].difficulty_simple_number +
						newValue.config[i].difficulty_general_number +
						newValue.config[i].difficulty_hard_number
					score += getScore(newValue.config[i])
					count += newValue.config[i].number
				} else {
					score += newValue.config[i].number * newValue.config[i].score
					count += newValue.config[i].number
				}
			}
			emit('updateSelectionConfig', JSON.stringify(newValue))
			emit('updateTotalScore', score) //总分
			emit('updateQuestionCount', count) //总数
		},
		{ deep: true }
	)

	const getScore = (item) => {
		if (!item.difficulty) {
			return item.number * item.score
		}
		return (
			item.difficulty_simple_number * item.difficulty_simple_score +
			item.difficulty_general_number * item.difficulty_general_score +
			item.difficulty_hard_number * item.difficulty_hard_score
		)
	}
</script>

<template>
	<a-card>
		<a-form-item-rest>
			<a-tree-select
				v-model:value="selectionConfigLocal.categoryIds"
				style="width: 100%"
				:tree-data="treeData"
				tree-checkable
				allow-clear
				:field-names="{
					children: 'children',
					label: 'name',
					value: 'id'
				}"
				:show-checked-strategy="SHOW_PARENT"
				search-placeholder="Please select"
			/>
			<div v-for="(item, index) in selectionConfigLocal.config" :key="index">
				<a-row>
					<a-space> {{ item.name }}：【选取</a-space>
					<a-space><a-input-number v-model:value="item.number" :min="0" />题， 每题 </a-space>
					<a-space> <a-input-number v-model:value="item.score" :min="0" />分】</a-space>
					<a-space>总分：{{ getScore(item) }}分</a-space>
					<a-button @click="item.difficulty = !item.difficulty">{{ item.difficulty ? '不分难度' : '分难度' }}</a-button>
				</a-row>
				<br />
				<a-card v-if="item.difficulty">
					<a-row>
						<a-space
							>*难度低，选取<a-input-number v-model:value="item.difficulty_simple_number" :min="0" />题， 每题
							<a-input-number v-model:value="item.difficulty_simple_score" :min="0" />分 总分：{{
								item.difficulty_simple_number * item.difficulty_simple_score
							}}分
						</a-space>
					</a-row>
					<br />
					<a-row>
						<a-space
							>*难度中，选取
							<a-input-number v-model:value="item.difficulty_general_number" :min="0" />
							题， 每题
							<a-input-number v-model:value="item.difficulty_general_score" :min="0" />分 总分：{{
								item.difficulty_general_number * item.difficulty_general_score
							}}分
						</a-space>
					</a-row>
					<br />
					<a-row>
						<a-space>
							*难度高，选取
							<a-input-number v-model:value="item.difficulty_hard_number" :min="0" />题， 每题
							<a-input-number v-model:value="item.difficulty_hard_score" :min="0" />分 总分：{{
								item.difficulty_hard_number * item.difficulty_hard_score
							}}分
						</a-space>
					</a-row>
				</a-card>
			</div>
		</a-form-item-rest>
	</a-card>
</template>

<style scoped lang="less"></style>
